package com.karry.yl.web.services;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.healthdata.HealthDataItem;
import com.karry.yl.domain.services.HandOver;
import com.karry.yl.domain.services.Items;
import com.karry.yl.service.healthdata.HealthDataItemService;
import com.karry.yl.service.services.HandOverService;
import com.karry.yl.service.services.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Administrator
 */
@Controller
@RequestMapping("/handover")
public class HandOverController extends SuperController {


    @Autowired
    private HandOverService handOverService;


    @RequestMapping
    public String listUI(){
        return "services/handover/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<HandOver> handOverPage = handOverService.selectPage(new Page<HandOver>(page, limit),new EntityWrapper<HandOver>().eq("orgId",currentUser.getOrgId()).eq("deleted",false).orderBy("handOverTime",false));
        return new LayTableResponse(handOverPage.getRecords(),handOverPage.getTotal());
    }

    /**
     * 跳转交接班的 弹窗页面
     * @return
     */
    @RequestMapping("/deal")
    public String deal(){
        return "services/handover/deal";
    }

    /**
     * 保存操作
     * @param handOver
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResponse save(HandOver handOver){
        handOver.setHandOverStart(HandOver.HANDOVERSTART_0);
        try {
            handOverService.insert(handOver);
            return  new JsonResponse(JsonResponse.CODE_SUCCESS,"成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResponse(JsonResponse.CODE_FAIL,"失败");
    }


    /**
     * 跳转接班接班的 弹窗页面
     * @return
     */
    @RequestMapping("/join")
    public String joinUI(Long id,ModelMap map){
        HandOver handOver = handOverService.selectById(id);
        map.put("handOver",handOver);
        return "services/handover/join";
    }

    /**
     * 跟新接班状态
     *
     * @param handOvers
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public JsonResponse update(HandOver handOvers){
        handOvers.setHandOverStart(HandOver.HANDOVERSTART_1);

        try {
            handOverService.updateById(handOvers);
            return  new JsonResponse(JsonResponse.CODE_SUCCESS,"成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResponse(JsonResponse.CODE_FAIL,"失败");
    }
}
